package com.offcn.dao;

import com.offcn.beans.User;
import com.offcn.utils.PageTool;

import java.util.List;

public class UserDao extends BaseDao<User>{
    public User adminLogin(String username, String password) {
        /*
            * 管理员登录
            * status: 1 启用状态
            * role: 1 管理员
            * */
        return singleLine("select * from user where username = ? and password = ? and status = 1 and role = 1", User.class, username, password);
    }

    public List<User> findAllUser(PageTool pageTool, String search) {
        String sql = "select * from user ";
        if (search != null && search.trim() != "") {
            sql+=" where name like '%"+search+"%' ";
        }
        sql+= " limit ?, ?";
        return moreLine(sql, User.class, pageTool.getStartIndex(), pageTool.getPageSize());
    }

    public int findTotalCount(String search) {
        String sql = "select count(*) from user ";
        if (search != null && search.trim() != "") {
            sql+=" where name like '%"+search+"%'";
        }
        Long count = (Long) singleData(sql);
        return count.intValue();
    }

    public int addUser(User user) {
        return dml("insert into user (name, phone, age, sex, username, password, status, createTime, role) values (?, ?, ?, ?, ?, ?, ?, now(), ?)",
                user.getName(), user.getPhone(), user.getAge(), user.getSex(), user.getUsername(), user.getPassword(), user.getStatus(), user.getRole());
    }
}
